// server.js
// where your node app starts

// init project
var express = require('express');
var app = express();

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
var cors = require('cors');
app.use(cors({optionsSuccessStatus: 200}));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (req, res) {
  res.sendFile(__dirname + '/views/index.html');
});


// your first API endpoint... 
app.get("/api/hello", function (req, res) {
  res.json({greeting: 'hello API'});
});
app.get("/api/", function (req, res) {
  const objDate = new Date();
    res.json({
        unix: objDate.valueOf(),
        utc: objDate.toUTCString()
      });
});
app.get("/api/:time", function (req, res) {
  const date_str = req.params.time;
  if (date_str) {
    if (/^\d{5,}/.test(date_str)) {
      const objDate = new Date(+date_str);
      if (objDate.toString() === 'Invalid Date') {
        res.json({
        error : "Invalid Date"
        });
      } else {
        res.json({
          unix: +date_str,
          utc: objDate.toUTCString()
        });
      }
    } else {
      const objDate = new Date(date_str);
      if (objDate.toString() === 'Invalid Date') {
        res.json({
        error : "Invalid Date"
        });
      } else {
        res.json({
        unix: objDate.valueOf(),
        utc: objDate.toUTCString()
      });
      }
    
    }
    
  } else {
    const objDate = new Date();
    res.json({
        unix: objDate.valueOf(),
        utc: objDate.toUTCString()
      });
  }
  // res.json({greeting: 'hello API'});
});

// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});
